package cn.pli.bike.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.widget.Toast;


import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import cn.pli.bike.R;
import cn.pli.bike.utils.EventBusAction;
import cn.pli.bike.utils.UtilData;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        api = WXAPIFactory.createWXAPI(this, UtilData.APP_ID);
        api.handleIntent(getIntent(), this);
        EventBus.getDefault().register(this);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {

        if (resp.errCode == 0) {
           /* switch (CommData.WXPAY_TYPE) {
                //充余额
                case CommData.PAY_DEPOSIT:
                    ConfirmPay(resp.errCode + "");
                    break;
                //商品购买
                case CommData.PAY_BUY:
                    ConfirmPay(resp.errCode + "");
                    break;
                //订单退款
                case CommData.PAY_CHARGE_BACK:
                    break;
                //提款操作
                case CommData.PAY_DRAWING:
                    break;
                //提现
                case CommData.PAY_WITHDRAW:
                    break;

            }*/
            Message message = new Message();
            message.what = EventBusAction.NOTICE_PAY_DEPOSIT;
            EventBus.getDefault().post(message);
            Toast.makeText(WXPayEntryActivity.this, "充值成功", Toast.LENGTH_LONG).show();//
            finish();

        } else if (resp.errCode == -2) {
            Toast.makeText(WXPayEntryActivity.this, "取消订单", Toast.LENGTH_LONG).show();//取消订单
            finish();
        } else if (resp.errCode == -1) {
            Toast.makeText(WXPayEntryActivity.this, "请重安装微信", Toast.LENGTH_LONG).show();//请重新安卓微信
            finish();
        } else {
            Toast.makeText(WXPayEntryActivity.this, "支付失败", Toast.LENGTH_LONG).show();//支付失败
            finish();
        }


    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handlePay(Message messageEvent) {

    }
}